package com.ztz.mynewdemo.utils;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 * Created by wqewqe on 2018/1/4.
 */

public class BaiDuLocationUtils {
    private BaiDuLocationUtils(){}
    static OnLoadListener mLoadListener;
    public static void create(Context context,OnLoadListener onLoadListener)
    {
        mLoadListener=onLoadListener;
        LocationClient locationClient=new LocationClient(context);
        locationClient.registerLocationListener(bdLocationListener);
        LocationClientOption option=new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd0911");
        option.setScanSpan(0);//仅定位一次
        locationClient.setLocOption(option);
        locationClient.start();
    }
    static BDLocationListener bdLocationListener=new BDLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if(bdLocation!=null) {
                if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation || bdLocation.getLocType() == BDLocation.TypeGpsLocation) {
                    if(mLoadListener!=null) {
                        mLoadListener.onLoadSucceed(bdLocation, bdLocation.getLatitude(), bdLocation.getLongitude());
                    }
                }
            }
        }
    };
    public interface OnLoadListener
    {
        void onLoadSucceed(BDLocation bdLocation,double latitude,double longitude);
    }
}
